/**
 * @author 周晨凯
 * @description 聊天信息的状态管理
 * 
 */

import {PayloadAction, createSlice,} from '@reduxjs/toolkit'

import {produce} from 'immer'
//用于数据上传
import { ImageUpload } from '@/server'
import Item from 'antd-mobile/es/components/dropdown/item'



export type infoType = {
    //数据类型
    type:'text' | 'image' | 'tips' | 'AI'| 'Prologue' | 'buttons',
    //数据内容
    value:string,
    //上传进度
    loadingPercent?:number,
    //上传是否成功
    isSuccess?:boolean,
    //图片的真实地址
    imagerealurl?:string
}
//发送数据的类型
export type MessageType = {
    //消息的id值
    id:string
    //头像
    head:string,
    //昵称
    nickname: string,
    //发送人id值
    sendid: string,
    //发送时间
    time: string,
    //发送内容
    info: infoType,
    //问题是否被解决，用于AI问题选项
    isdelete?: 'yes' | 'no' | 'null'
}



const InitialMessage:MessageType[] = []

const MessageSlice = createSlice({
    name:'MessageSlice',
    initialState:InitialMessage,
    reducers:{
        getMessageList(state,action:PayloadAction<string>){
            const id = action.payload;
            if(state.length === 0) return undefined
            return state.filter((item)=>{
                return item.sendid === id
            })
        },
        setMessageList:produce((draft:MessageType[],action:PayloadAction<MessageType>)=>{
            draft.push(action.payload)
        }),

        setHistoryMessageList:produce((draft:MessageType[],action:PayloadAction<MessageType[]>)=>{
            action.payload.forEach((Item)=>{
                draft.unshift(Item)
            })
        }),

        getMessageListById:produce((draft:MessageType[],action:PayloadAction<{id:string,loadingPercent:number}>)=>{
            const {id,loadingPercent} = action.payload
            const index = draft.findIndex((item)=>item.id === id)
            draft[index].info.loadingPercent = loadingPercent
            if(loadingPercent === 100){
                draft[index].info.isSuccess = true
            }
        }),
        //设置AI的实时消息
        setAIMessageListByid:produce((draft:MessageType[],action:PayloadAction<MessageType>)=>{
            const {id} = action.payload
            const index = draft.findIndex((item)=>item.id === id)
            if(index === -1){
                draft.push(action.payload)
            }else{
                draft[index].info.value = action.payload.info.value
            }
        }),

        //设置AI的问题是否被解决
        setAIMessageIsDeleteByid:produce((draft:MessageType[],action:PayloadAction<{id:string,isdelete: 'yes' | 'no'}>)=>{
            const {id} = action.payload
            const index = draft.findIndex((item)=>item.id === id)
            draft[index].isdelete = action.payload.isdelete
        }),

        setMessageByIdUrl:produce((draft:MessageType[],action:PayloadAction<{id:string,url:string}>)=>{
            const {url,id} = action.payload
            const index = draft.findIndex((item)=>item.id === id)
            
            draft[index].info.imagerealurl = url
        })
    },

})


export const {getMessageList,setMessageList,getMessageListById,setMessageByIdUrl,setAIMessageListByid,setAIMessageIsDeleteByid,setHistoryMessageList} = MessageSlice.actions

export default MessageSlice.reducer